// pages/application-header/application-header.js
import api from '../../utils/request/api'
import {
  applyTeam
} from '../../utils/request/apiurl'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeList: [
      "未申请", "审核中", "审核已通过", "审核未通过"
    ],
    type: 0,
	uid:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //获取申请状态
    api.post(applyTeam.type, {
      uid: this.data.uid||wx.getStorageSync('userInfo').uid
    }).then(res => {
      // console.log(res)
      this.setData({
        type: res
      })
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
	var userInfo=wx.getStorageSync('userInfo').uid
	this.setData({
		uid:userInfo.uid
	})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
	wx.showNavigationBarLoading() //在标题栏中显示加载
	
	this.onLoad()
	setTimeout(function() {
		wx.hideNavigationBarLoading() //完成停止加载
		wx.stopPullDownRefresh() //停止下拉刷新
	}, 1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //确认申请
  applicationHeader: function(e) {
    let type = this.data.type
    let form = e.detail.value
    form.uid = this.data.uid||wx.getStorageSync('userInfo').uid
    // console.log(form)
    let noPass = false
    for (let a in form) {
      if (!form[a]) {
        noPass = true
      }
    }
    if (!noPass) {
      api.post(applyTeam.apply, form).then(res => {
        wx.showToast({
          title: '申请成功',
          icon: 'success',
		  duration:2000,
          complete: function() {
            wx.switchTab({
              url: '../personal-center/personal-center'
            })
          }
        })
      }).catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none'
        })
      })
    } else {
      wx.showToast({
        title: "请完整填写申请资料",
        icon: 'none',
		duration:2000
      })
    }
  },
  //重新申请
  reApply() {
    this.setData({
      type: 0
    })
  }
})